﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace BF_160
{
    public partial class MyButton : UserControl
    {
        private EventHandler mOnClicked; 

        private float Btnpercent;
        public float BtnPercent
        {
            get { return Btnpercent; }
            set
            {
                Btnpercent = value;
                Invalidate();
            }
        }
      
        private string Btntext;
        public string BtnText 
        {
            get { return Btntext; }
            set { 
                Btntext = value;
                Invalidate();
            }
        }
        private  Color OldBtnStartcolor1;
        private Color OldBtnEndcolor1;
        private Color OldBtnStartcolor2;
        private Color OldBtnEndcolor2;
        private bool enable;
        public bool Enable
        {
            get { return enable; }
            set
            {
              enable = value;
                if (enable == false)
                {
                    BtnStartcolor1 = DarkerColor(OldBtnStartcolor1, 80f);
                    BtnEndcolor1 = DarkerColor(OldBtnEndcolor1, 80f);
                    BtnStartcolor2 = DarkerColor(OldBtnStartcolor2, 80f);
                    BtnEndcolor2 = DarkerColor(OldBtnEndcolor2, 80f);

                }
                else
                {
                    BtnStartcolor1 = OldBtnStartcolor1;
                    BtnEndcolor1   = OldBtnEndcolor1;
                    BtnStartcolor2 = OldBtnStartcolor2;
                    BtnEndcolor2   = OldBtnEndcolor2 ;
                }
                Invalidate();
            }
        }
        private int Btnno;
        public int BtnNo
        {
            get { return Btnno; }

            set { Btnno = value; }
        }
        private  Color BtnStartcolor1;
        public  Color BtnStartColor1
        {
            get { return BtnStartcolor1; }
            set
            {
                BtnStartcolor1 = value;
                OldBtnStartcolor1 = value;
            }
        }

        private Color BtnEndcolor1;
        public Color BtnEndColor1
        {
            get { return BtnEndcolor1; }
            set
            {
                BtnEndcolor1 = value;
                OldBtnEndcolor1 = value;
            }
        }

        private Color BtnStartcolor2;
        public Color BtnStartColor2
        {
            get { return BtnStartcolor2; }
            set
            {
                BtnStartcolor2 = value;
                OldBtnStartcolor2 = value;
            }
        }

        private Color BtnEndcolor2;
        public Color BtnEndColor2
        {
            get { return BtnEndcolor2; }
            set
            {
                BtnEndcolor2 = value;
                OldBtnEndcolor2 = value;
            }
        }
        public bool Btncheck;
        public bool BtnCheck
        {
            get { return Btncheck; }
            set { Btncheck = value; }
        }
        public int Btnsize;
        public int BtnSize
        {
            get { return Btnsize; }
            set { BtnSize = value; }
        }
         public MyButton()
        {
            InitializeComponent();      
          
        }
        private Color DarkerColor(Color color, float correctionfactory = 50f)
        {
            const float hundredpercent = 100f;
            return Color.FromArgb((int)(((float)color.R / hundredpercent) * correctionfactory),
                (int)(((float)color.G / hundredpercent) * correctionfactory), (int)(((float)color.B / hundredpercent) * correctionfactory));
        }
        protected   override   void   OnClick(EventArgs   e) 
       {
            if (enable == false) return;
           if (Btncheck) Btncheck = false;
           else Btncheck = true;

           base.OnClick(e);
           if (mOnClicked != null)
           {
               mOnClicked.Invoke(this, EventArgs.Empty);
           }
           Invalidate();

       }
      public event EventHandler ClickEventHandler
        {
            add
            {
                mOnClicked += value;
            }
            remove
            {
                mOnClicked = value;
            }
        }

      private void MyButton_Paint(object sender, PaintEventArgs e)
        {
            GraphicsPath gp = new GraphicsPath();
            gp.AddEllipse(4, 4, Btnsize-7, Btnsize-7);
            Rectangle rect = new Rectangle(0, 0, Btnsize+1, Btnsize+1);
            LinearGradientBrush b2 = new LinearGradientBrush(rect, BtnStartcolor1, BtnEndcolor1, LinearGradientMode.Vertical);
            LinearGradientBrush b1 = new LinearGradientBrush(rect, BtnStartcolor2, BtnEndcolor2, LinearGradientMode.Vertical);
 
            Graphics g = e.Graphics;
            //rect = this.ClientRectangle;
            float a1=Btnpercent*360.0f;
            g.FillPie(b1, rect, 270.0f, a1);
            g.FillPie(b2, rect, 270.0f+a1, 360.0f-a1);
            Brush b3 = new SolidBrush(Color.Blue);
            Brush b4 = new SolidBrush(Color.White);
            Brush b5 = new SolidBrush(Color.Black);
            g.DrawString(BtnText, new Font("Arial",24), b4,new PointF(26, 18));
            g.DrawString(BtnText, new Font("Arial", 24), b5, new PointF(28, 20));
            g.DrawString(BtnText, new Font("Arial", 24), b3, new PointF(27, 19));
            Rectangle rec1 = new Rectangle(42,58,16,16);
            Rectangle rec2 = new Rectangle(44,60,13,13);
            LinearGradientBrush b6 = new LinearGradientBrush(rec1,  Color.LightBlue, Color.Blue,LinearGradientMode.Vertical); 
            Pen pen = new Pen(b6);
            g.DrawRectangle(pen, rec1);
            if(Btncheck)g.FillRectangle(b6, rec2);
            g.Flush();

            this.Region = new Region(gp);
        }

         private void MyButton_Load(object sender, EventArgs e)
        {
            this.Size = new System.Drawing.Size(Btnsize, Btnsize);
        }
    }
}

